package com.kiln.juxingqs.ui.vm

import android.app.Application
import android.text.TextUtils

import androidx.lifecycle.MutableLiveData
import client.global.commlib.ContextProvider
import client.global.commlib.network.data.JsonResult
import client.global.commlib.network.data.Ret
import client.global.commlib.toast.toast
import client.global.commlib.utils.Util
import com.kiln.juxingqs.ui.viewmodel.MiddleVm


class WjmmViewModel(app: Application) : MiddleVm(app) {


    val codeRet = MutableLiveData<JsonResult<Ret>>()
    val isShowing = MutableLiveData<Boolean>(false)


    val account = MutableLiveData<String>("")
    val username = MutableLiveData<String>("")
    val password = MutableLiveData<String>("")
    val smscode = MutableLiveData<String>("")
    val getCodeTxt = MutableLiveData<String>("获取验证码")
    val passwordAgain = MutableLiveData<String>()
    val isShowPwd = MutableLiveData<Boolean>(false)
    val isShowPwdAgain = MutableLiveData<Boolean>(false)
    val getCodeEnable = MutableLiveData<Boolean>(true)
    val account_new = MutableLiveData<String>("")

    val okRet = MutableLiveData<JsonResult<Ret>>()


    fun switchIsShowPwd() {
        isShowPwd.value = !isShowPwd.value!!
    }

    fun switchIsShowPwdAgain() {
        isShowPwdAgain.value = !isShowPwdAgain.value!!
    }


    fun ok_update_phone() {
        hold({
            server.update_phone(
                account.value!!,
                smscode.value!!,
                account_new.value!!,
            )
        }, {
            isShowing.value = true
        }, {
            okRet.value = it
        }, complete = {
            isShowing.value = false
        })

    }


    fun ok_update_real() {
        hold({
            server.update_real(
                account.value!!,
                smscode.value!!,
            )
        }, {
            isShowing.value = true
        }, {
            okRet.value = it
        }, complete = {
            isShowing.value = false
        })

    }

    fun ok_update_logout() {
        hold({
            server.update_logout(
                account.value!!,
                smscode.value!!,
            )
        }, {
            isShowing.value = true
        }, {
            okRet.value = it
        }, complete = {
            isShowing.value = false
        })

    }

    /**
     *
     */
    fun ok() {

        if (TextUtils.isEmpty(username.value) || TextUtils.isEmpty(account.value) || TextUtils.isEmpty(
                password.value
            ) || TextUtils.isEmpty(
                passwordAgain.value
            ) || TextUtils.isEmpty(smscode.value)
        ) {
            toast("所有不能为空")
            return
        }

        if (!password.value.equals(passwordAgain.value)) {
            toast("二次输入密码不一致")
            return
        }

        hold({
            server.forget_pwd(
                username.value!!,
                account.value!!,
                smscode.value!!,
                password.value!!,
                passwordAgain.value!!
            )
        }, {
            isShowing.value = true
        }, {
            toast("找回成功")
            ContextProvider.get().activity?.apply {
                finish()
            }
        }, complete = {
            isShowing.value = false

        })

    }


    fun getCode() {

        if (TextUtils.isEmpty(account.value)) {
            toast("手机号不能为空")
            return
        }
        hold({
            val ts = System.currentTimeMillis()
            server.getcode(
                account.value!!,
                "${ts}", Util.getCaptchaSign(account.value!!, "${ts}")
            )
        }, {
            isShowing.value = true
            getCodeEnable.value = false
            codeRet.value = null
        }, {
            codeRet.value = it
        }, complete = {
            isShowing.value = false
        }, err = {
            getCodeEnable.value = true
        })

    }
}